<!DOCTYPE html>
<html>

<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
	<meta name="theme-color" content="#33474d">
	<title>Linux 命令- sed | 失落的乐章</title>
	<link rel="stylesheet" href="/css/style.css" />
	
      <link rel="alternate" href="/atom.xml" title="失落的乐章" type="application/atom+xml">
    
</head>

<body>

	<header class="header">
		<nav class="header__nav">
			
				<a href="/archives" class="header__link">Archive</a>
			
				<a href="/tags" class="header__link">Tags</a>
			
				<a href="/atom.xml" class="header__link">RSS</a>
			
		</nav>
		<h1 class="header__title"><a href="/">失落的乐章</a></h1>
		<h2 class="header__subtitle">技术面前，永远都是学生。</h2>
	</header>

	<main>
		<article>
	
		<h1>Linux 命令- sed</h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/Linux命令/">Linux命令</a> <a class="article__tag-link" href="/tags/正则表达式/">正则表达式</a>
			</span>
		
	</div>

	

	
		<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;sed 命令是利用 script 来处理文本文件。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;sed 可依照 script 的指令来处理、编辑文本文件。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;sed 主要用来自动编辑一个或多个文件；简化对文件的反复操作；编写转换程序等。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;sed 可以实现 grep 的大部分功能，而且还可以查找替换。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;sed 跟 grep 一样，不识别 + 、| 、｛｝、 （）等符号，需要借助脱义符号 、 或者使用选项 -r</p>
<h2 id="1-命令语法"><a href="#1-命令语法" class="headerlink" title="1. 命令语法"></a>1. 命令语法</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">sed [-hnV] [-e&lt;script&gt;] [-f&lt;script文件&gt;] [文本文件]</div></pre></td></tr></table></figure>
<h2 id="2-命令参数"><a href="#2-命令参数" class="headerlink" title="2. 命令参数"></a>2. 命令参数</h2><ul>
<li>-e <code>&lt;script&gt;或--expression=&lt;script&gt;</code> 以选项中指定的script来处理输入的文本文件。</li>
<li>-f <script文件>或–file=<script文件> 以选项中指定的script文件来处理输入的文本文件。</script文件></script文件></li>
<li>-h 或–help 显示帮助。</li>
<li>-n 或–quiet或–silent 仅显示script处理后的结果。</li>
<li>-V 或–version 显示版本信息。</li>
</ul>
<h2 id="3-命令动作"><a href="#3-命令动作" class="headerlink" title="3. 命令动作"></a>3. 命令动作</h2><ul>
<li>a ：新增， a 的后面可以接字串，而这些字串会在新的一行出现(目前的下一行)～</li>
<li>c ：取代， c 的后面可以接字串，这些字串可以取代 n1,n2 之间的行！</li>
<li>d ：删除，因为是删除啊，所以 d 后面通常不接任何咚咚；</li>
<li>i ：插入， i 的后面可以接字串，而这些字串会在新的一行出现(目前的上一行)；</li>
<li>p ：列印，亦即将某个选择的数据印出。通常 p 会与参数 sed -n 一起运行～</li>
<li>s ：取代，可以直接进行取代的工作哩！通常这个 s 的动作可以搭配正规表示法！例如 1,20s/old/new/g 就是啦！</li>
</ul>
<h2 id="4-使用实例"><a href="#4-使用实例" class="headerlink" title="4.使用实例"></a>4.使用实例</h2><h3 id="实例1：在-testfile-文件的第四行后添加一行，并将结果输出到标准输出"><a href="#实例1：在-testfile-文件的第四行后添加一行，并将结果输出到标准输出" class="headerlink" title="实例1：在 testfile 文件的第四行后添加一行，并将结果输出到标准输出"></a>实例1：在 testfile 文件的第四行后添加一行，并将结果输出到标准输出</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>命令</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">sed -e 4a \newLine testfile</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>输出</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div></pre></td><td class="code"><pre><div class="line">$ cat testfile </div><div class="line">HELLO LINUX!  </div><div class="line">Linux is a free unix-type opterating system.  </div><div class="line">This is a linux testfile!  </div><div class="line">Linux <span class="built_in">test</span> </div><div class="line">$ sed -e 4a\newline testfile </div><div class="line">HELLO LINUX!   </div><div class="line">Linux is a free unix-type opterating system.  </div><div class="line">This is a linux testfile!  </div><div class="line">Linux <span class="built_in">test</span>  </div><div class="line">newline</div></pre></td></tr></table></figure>
<h2 id="实例2：将-etc-passwd-的内容列出并且列印行号，同时将第2-5行删除"><a href="#实例2：将-etc-passwd-的内容列出并且列印行号，同时将第2-5行删除" class="headerlink" title="实例2：将 /etc/passwd 的内容列出并且列印行号，同时将第2-5行删除"></a>实例2：将 /etc/passwd 的内容列出并且列印行号，同时将第2-5行删除</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>命令</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">nl /etc/passwd | sed <span class="string">'2,5d'</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>输出</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div></pre></td><td class="code"><pre><div class="line">[root@localhost ~]<span class="comment"># nl /etc/passwd|sed '2,5d'</span></div><div class="line">     1  root:x:0:0:root:/root:/bin/bash</div><div class="line">     6  sync:x:5:0:sync:/sbin:/bin/sync</div><div class="line">     7  shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown</div><div class="line">     8  halt:x:7:0:halt:/sbin:/sbin/halt</div><div class="line">     9  mail:x:8:12:mail:/var/spool/mail:/sbin/nologin</div><div class="line">    10  operator:x:11:0:operator:/root:/sbin/nologin</div><div class="line">    11  games:x:12:100:games:/usr/games:/sbin/nologin</div><div class="line">    12  ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin</div><div class="line">    13  nobody:x:99:99:Nobody:/:/sbin/nologin</div><div class="line">    14  systemd-bus-proxy:x:999:997:systemd Bus Proxy:/:/sbin/nologin</div><div class="line">    15  systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin</div><div class="line">    16  dbus:x:81:81:System message bus:/:/sbin/nologin</div><div class="line">    17  polkitd:x:998:996:User <span class="keyword">for</span> polkitd:/:/sbin/nologin</div><div class="line">    18  tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin</div><div class="line">    19  postfix:x:89:89::/var/spool/postfix:/sbin/nologin</div><div class="line">    20  sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin</div><div class="line">    21  chrony:x:997:995::/var/lib/chrony:/sbin/nologin</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>说明</strong></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;以行为单位的删除，sed 的动作为 ‘2,5d’，那个 d 就是删除。因为 2-5 行给删除了，所以显示的数据就没有 2-5 行。另外，注意一下，原本应该是要下达 -e 才对，没有 -e 也可以。同时也要注意的是，sed 后面接的动作务必以 ‘’单引号括住。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;只要删除第2行</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">nl /etc/passwd | sed <span class="string">'2d'</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;要删除第3到最后一行</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">nl /etc/passwd |sed <span class="string">'3,$d'</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;在第2行后，第3行上加上 ‘drink tea’ 字样</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">nl /etc/passwd | sed <span class="string">'2a drink tea'</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;如果要加再第2行前</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">nl /etc/passwd | sed <span class="string">'2i drink tea'</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;如果要在第2行后面加入两行</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">nl /etc/passwd | sed <span class="string">'2a drink tea \</span></div><div class="line"><span class="string">&gt; drink beer'</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;每一行之间都必须要以反斜杠（\）来进行新行的添加</p>
<h3 id="实例3：将第2-5行内容取代成为‘2-5-number’"><a href="#实例3：将第2-5行内容取代成为‘2-5-number’" class="headerlink" title="实例3：将第2-5行内容取代成为‘2-5 number’"></a>实例3：将第2-5行内容取代成为‘2-5 number’</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>命令</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">nl /etc/passwd | sed <span class="string">'2,5c 2-5 number'</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>输出</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div></pre></td><td class="code"><pre><div class="line">[root@localhost ~]<span class="comment"># nl /etc/passwd | sed '2,5c 2-5 number'</span></div><div class="line">     1  root:x:0:0:root:/root:/bin/bash</div><div class="line">2-5 number</div><div class="line">     6  sync:x:5:0:sync:/sbin:/bin/sync</div><div class="line">     7  shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown</div><div class="line">     8  halt:x:7:0:halt:/sbin:/sbin/halt</div><div class="line">     9  mail:x:8:12:mail:/var/spool/mail:/sbin/nologin</div><div class="line">    10  operator:x:11:0:operator:/root:/sbin/nologin</div><div class="line">    11  games:x:12:100:games:/usr/games:/sbin/nologin</div><div class="line">    12  ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin</div><div class="line">    13  nobody:x:99:99:Nobody:/:/sbin/nologin</div><div class="line">    14  systemd-bus-proxy:x:999:997:systemd Bus Proxy:/:/sbin/nologin</div><div class="line">    15  systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin</div><div class="line">    16  dbus:x:81:81:System message bus:/:/sbin/nologin</div><div class="line">    17  polkitd:x:998:996:User <span class="keyword">for</span> polkitd:/:/sbin/nologin</div><div class="line">    18  tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin</div><div class="line">    19  postfix:x:89:89::/var/spool/postfix:/sbin/nologin</div><div class="line">    20  sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin</div><div class="line">    21  chrony:x:997:995::/var/lib/chrony:/sbin/nologin</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>说明</strong></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;以行为单位的替换与显示，通过这个方法就能够将数据整行取代了。</p>
<h3 id="实例4：仅列出-etc-passwd-文件内的5-7-行"><a href="#实例4：仅列出-etc-passwd-文件内的5-7-行" class="headerlink" title="实例4：仅列出 /etc/passwd 文件内的5-7 行"></a>实例4：仅列出 /etc/passwd 文件内的5-7 行</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>命令</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">nl /etc/passwd |sed -n <span class="string">'5,7p'</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>输出</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line">[root@localhost ~]<span class="comment"># nl /etc/passwd | sed -n '5,7p'</span></div><div class="line">     5  lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin</div><div class="line">     6  sync:x:5:0:sync:/sbin:/bin/sync</div><div class="line">     7  shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>说明</strong><br>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;如果 root 找到，处理输出所有行，还回输出匹配行。<br>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;使用 -n 的时候将只打印包含模版的行</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">[root@localhost ~]<span class="comment"># nl /etc/passwd | sed -n '/root/p'</span></div><div class="line">     1  root:x:0:0:root:/root:/bin/bash</div><div class="line">    10  operator:x:11:0:operator:/root:/sbin/nologin</div></pre></td></tr></table></figure>
<h3 id="实例5：删除-etc-paawd-所有包含-root-的行，其他行输出"><a href="#实例5：删除-etc-paawd-所有包含-root-的行，其他行输出" class="headerlink" title="实例5：删除 /etc/paawd 所有包含 root 的行，其他行输出"></a>实例5：删除 /etc/paawd 所有包含 root 的行，其他行输出</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>命令</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">nl /etc/passwd | sed <span class="string">'/root/d'</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>输出</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div></pre></td><td class="code"><pre><div class="line">[root@localhost ~]<span class="comment"># nl /etc/passwd | sed '/root/d'</span></div><div class="line">     2  bin:x:1:1:bin:/bin:/sbin/nologin</div><div class="line">     3  daemon:x:2:2:daemon:/sbin:/sbin/nologin</div><div class="line">     4  adm:x:3:4:adm:/var/adm:/sbin/nologin</div><div class="line">     5  lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin</div><div class="line">     6  sync:x:5:0:sync:/sbin:/bin/sync</div><div class="line">     7  shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown</div><div class="line">     8  halt:x:7:0:halt:/sbin:/sbin/halt</div><div class="line">     9  mail:x:8:12:mail:/var/spool/mail:/sbin/nologin</div><div class="line">    11  games:x:12:100:games:/usr/games:/sbin/nologin</div><div class="line">    12  ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin</div><div class="line">    13  nobody:x:99:99:Nobody:/:/sbin/nologin</div><div class="line">    14  systemd-bus-proxy:x:999:997:systemd Bus Proxy:/:/sbin/nologin</div><div class="line">    15  systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin</div><div class="line">    16  dbus:x:81:81:System message bus:/:/sbin/nologin</div><div class="line">    17  polkitd:x:998:996:User <span class="keyword">for</span> polkitd:/:/sbin/nologin</div><div class="line">    18  tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin</div><div class="line">    19  postfix:x:89:89::/var/spool/postfix:/sbin/nologin</div><div class="line">    20  sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin</div><div class="line">    21  chrony:x:997:995::/var/lib/chrony:/sbin/nologin</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>说明</strong></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;数据的搜寻并删除</p>
<h3 id="实例6：搜索-etc-passwd-找到-root-对应的行，执行后面花括号中的命令，每个命令之间用分号分割，并把bash替换为blueshell，在输出这行"><a href="#实例6：搜索-etc-passwd-找到-root-对应的行，执行后面花括号中的命令，每个命令之间用分号分割，并把bash替换为blueshell，在输出这行" class="headerlink" title="实例6：搜索 /etc/passwd 找到 root 对应的行，执行后面花括号中的命令，每个命令之间用分号分割，并把bash替换为blueshell，在输出这行"></a>实例6：搜索 /etc/passwd 找到 root 对应的行，执行后面花括号中的命令，每个命令之间用分号分割，并把bash替换为blueshell，在输出这行</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>命令</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">nl /etc/passwd | sed -n <span class="string">'/bash/ &#123;s/bash/blueshell/;p;q&#125;'</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>输出</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">[root@localhost ~]<span class="comment"># nl /etc/passwd | sed -n '/bash/ &#123;s/bash/blueshell/;p;q&#125;'</span></div><div class="line">     1  root:x:0:0:root:/root:/bin/blueshell</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>说明</strong></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;数据的搜寻并执行命令，最后的q是退出</p>
<h3 id="实例7：查找-etc-passwd-内-root-的行，并把-root-替换为-shell"><a href="#实例7：查找-etc-passwd-内-root-的行，并把-root-替换为-shell" class="headerlink" title="实例7：查找 /etc/passwd 内 root 的行，并把 root 替换为 shell"></a>实例7：查找 /etc/passwd 内 root 的行，并把 root 替换为 shell</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>命令</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">sed <span class="string">'s/root/shell/g'</span> /etc/passwd</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>输出</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div></pre></td><td class="code"><pre><div class="line">[root@localhost ~]<span class="comment"># sed 's/root/shell/g' /etc/passwd</span></div><div class="line">shell:x:0:0:shell:/shell:/bin/bash</div><div class="line">bin:x:1:1:bin:/bin:/sbin/nologin</div><div class="line">daemon:x:2:2:daemon:/sbin:/sbin/nologin</div><div class="line">adm:x:3:4:adm:/var/adm:/sbin/nologin</div><div class="line">lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin</div><div class="line">sync:x:5:0:sync:/sbin:/bin/sync</div><div class="line">shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown</div><div class="line">halt:x:7:0:halt:/sbin:/sbin/halt</div><div class="line">mail:x:8:12:mail:/var/spool/mail:/sbin/nologin</div><div class="line">operator:x:11:0:operator:/shell:/sbin/nologin</div><div class="line">games:x:12:100:games:/usr/games:/sbin/nologin</div><div class="line">ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin</div><div class="line">nobody:x:99:99:Nobody:/:/sbin/nologin</div><div class="line">systemd-bus-proxy:x:999:997:systemd Bus Proxy:/:/sbin/nologin</div><div class="line">systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin</div><div class="line">dbus:x:81:81:System message bus:/:/sbin/nologin</div><div class="line">polkitd:x:998:996:User <span class="keyword">for</span> polkitd:/:/sbin/nologin</div><div class="line">tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin</div><div class="line">postfix:x:89:89::/var/spool/postfix:/sbin/nologin</div><div class="line">sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin</div><div class="line">chrony:x:997:995::/var/lib/chrony:/sbin/nologin</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>说明</strong></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;数据的搜寻并替换，s 就是替换的意思，g 为全局替换，否则只替换一次。</p>
<h3 id="实例8：使用-ifconfig-查看ip地址，并提取出ip地址"><a href="#实例8：使用-ifconfig-查看ip地址，并提取出ip地址" class="headerlink" title="实例8：使用 ifconfig 查看ip地址，并提取出ip地址"></a>实例8：使用 ifconfig 查看ip地址，并提取出ip地址</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;先使用 ifconfig 命令查询eth0网卡ip</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div></pre></td><td class="code"><pre><div class="line">[root@localhost ~]<span class="comment"># ifconfig eth0</span></div><div class="line">eth0      Link encap:Ethernet  HWaddr 00:0C:29:4D:9F:F7  </div><div class="line">          inet addr:192.168.0.73  Bcast:192.168.0.255  Mask:255.255.255.0</div><div class="line">          inet6 addr: fe80::20c:29ff:fe4d:9ff7/64 Scope:Link</div><div class="line">          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1</div><div class="line">          RX packets:41 errors:0 dropped:0 overruns:0 frame:0</div><div class="line">          TX packets:48 errors:0 dropped:0 overruns:0 carrier:0</div><div class="line">          collisions:0 txqueuelen:1000 </div><div class="line">          RX bytes:4525 (4.4 KiB)  TX bytes:5868 (5.7 KiB)</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>命令</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">ifconfig eth0 | grep <span class="string">'inet addr'</span> | sed <span class="string">'s/^.*addr://g'</span> | sed <span class="string">'s/Bcast.*$//g'</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>输出</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">ifconfig eth0 | grep <span class="string">'inet addr'</span> | sed <span class="string">'s/^.*addr://g'</span> | sed <span class="string">'s/Bcast.*$//g'</span></div><div class="line">192.168.0.73</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>说明</strong></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;数据的搜寻并替换，先观察原始信息，利用 ficonfig 查询 eth0 网卡 ip，再用 sed 命令将 ip 前面的部分删除（就是把前面的部分替换为空），接下来是删除后续的部分（把后续部分替换为空）</p>
<h3 id="实例9：删除-etc-passwd-第3行到末尾的数据，并把bash-替换为-blueshell"><a href="#实例9：删除-etc-passwd-第3行到末尾的数据，并把bash-替换为-blueshell" class="headerlink" title="实例9：删除 /etc/passwd 第3行到末尾的数据，并把bash 替换为 blueshell"></a>实例9：删除 /etc/passwd 第3行到末尾的数据，并把bash 替换为 blueshell</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>命令</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">nl /etc/passwd | sed -e <span class="string">'3,$d'</span> -e <span class="string">'s/bash/blueshell/g'</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>输出</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line">[root@localhost ~]<span class="comment"># nl /etc/passwd | sed -e '3,$d' -e 's/bash/blueshell/g'</span></div><div class="line"></div><div class="line">     1  root:x:0:0:root:/root:/bin/blueshell</div><div class="line">     2  bin:x:1:1:bin:/bin:/sbin/nologin</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>说明</strong></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;多点编辑，-e 表示多点编辑，第一个编辑命令删除 /etc/passwd 第三行到末行的数据，第二条命令搜索 bash 替换为 blueshell</p>
<h3 id="实例10：直接修改文件内容（危险动作）"><a href="#实例10：直接修改文件内容（危险动作）" class="headerlink" title="实例10：直接修改文件内容（危险动作）"></a>实例10：直接修改文件内容（危险动作）</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;sed 可以直接修改文件的内容，不必使用管道命令或数据流重定向。不过，由于这个动作会直接修改到原始的文件，所以千万不要随便拿系统配置文件来测试!</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;先用 cp 命令拷贝一份/etc/passwd 文件为 1.txt</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@localhost ~]<span class="comment"># cp /etc/passwd 1.txt</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;删除所有非数字</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div></pre></td><td class="code"><pre><div class="line">[root@localhost ~]<span class="comment"># sed -i 's/[^0-9]//g' 1.txt</span></div><div class="line">[root@localhost ~]<span class="comment"># cat 1.txt</span></div><div class="line">00</div><div class="line">11</div><div class="line">22</div><div class="line">34</div><div class="line">47</div><div class="line">50</div><div class="line">60</div><div class="line">70</div><div class="line">812</div><div class="line">110</div><div class="line">12100</div><div class="line">1450</div><div class="line">9999</div><div class="line">999997</div><div class="line">192192</div><div class="line">8181</div><div class="line">998996</div><div class="line">5959</div><div class="line">8989</div><div class="line">7474</div><div class="line">997995</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>说明</strong></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;sed 的 -i 选项可以直接修改文件内容，这功能非常有帮助！举例来说，如果有一个100万行的文件，要在第100行加某些文字，此时使用 vim 可能会疯掉！因为文件太大了！就利用 sed ，通过 sed 直接修改、取代的功能，甚至不用使用 vim 去修订。</p>

	

	
		<span class="different-posts"><a href="/2017/10/12/2. Linux 命令/70. Linux 命令- sed/" onclick="window.history.go(-1); return false;">⬅️ Go back </a></span>

	

</article>

	</main>

	<footer class="footer">
	<div class="footer-content">
		
	      <div class="footer__element">
	<p>Hi there, <br />welcome to my Blog glad you found it. Have a look around, will you?</p>
</div>

	    
	      <div class="footer__element">
	<h5>Check out</h5>
	<ul class="footer-links">
		<li class="footer-links__link"><a href="/archives">Archive</a></li>
		
		  <li class="footer-links__link"><a href="/atom.xml">RSS</a></li>
	    
		<li class="footer-links__link"><a href="/about">about page</a></li>
		<li class="footer-links__link"><a href="/tags">Tags</a></li>
		<li class="footer-links__link"><a href="/categories">Categories</a></li>
	</ul>
</div>

	    

		<div class="footer-credit">
			<span>© 2017 失落的乐章 | Powered by <a href="https://hexo.io/">Hexo</a> | Theme <a href="https://github.com/HoverBaum/meilidu-hexo">MeiliDu</a></span>
		</div>

	</div>


</footer>



</body>

</html>
